#ifndef _UI_MANAGER_H_
#define _UI_MANAGER_H_

#include "esp_err.h"
#include "lvgl.h"
#include "slist.h"

#ifdef __cplusplus
extern "C" {
#endif

#define TRANSMIT_ALL_KEY_EVENT  0xFFFFFFFF

typedef int (*ui_view_proc_t)(uint8_t view_id, uint8_t msg_id, uint32_t msg_data);
typedef int (*ui_get_state_t)(void);
typedef lv_obj_t* (*ui_lvgl_register)(void);

enum APP_UI_VIEW_ID {
    MAIN_VIEW,
	MENU_VIEW,
	BTMUSIC_VIEW,
	LCMUSIC_VIEW,
	RECORD_VIEW,

	UI_VIEW_MAX,
};

typedef struct {
	uint8_t view_id;
	lv_obj_t* lv_obj;
	ui_lvgl_register lvgl_register;
} ui_view_lvgl_t;

typedef struct {
	uint32_t key_code;
	uint32_t key_type; 
	uint32_t app_state;
	uint32_t msg_cmd;
} ui_key_map_t;

typedef struct {
	uint16_t order;
	char* task_name;
    lv_obj_t* src_obj;
    const ui_key_map_t* view_key_map;
	ui_view_proc_t  view_proc;
	ui_get_state_t  view_get_state;
} ui_view_info_t;

typedef struct {
	sys_snode_t node;
	uint8_t view_id;
	char* task_name;
	ui_view_info_t info;
} ui_view_context_t;

typedef struct {
	sys_slist_t view_list;
	ui_view_lvgl_t* view_lvgl_list;
	uint32_t update_region;
} ui_manager_context_t;

enum UI_MSG_ID {
	MSG_VIEW_CREATE = 0,
	MSG_VIEW_DELETE,
	MSG_VIEW_PAINT,
	MSG_SET_FOCUS,
	MSG_KILL_FOCUS,
	MSG_KEY_EVENT,
	MSG_VIEW_RECOVER,
};

bool ui_view_create(uint8_t view_id, ui_view_info_t *info);
bool ui_view_delete(uint8_t view_id);
int ui_message_send_async(uint8_t view_id, uint32_t msg_id, uint32_t msg_data);
int ui_message_dispatch(uint8_t view_id, uint32_t msg_id, uint32_t msg_data);
int ui_manager_dispatch_key_event(uint32_t key_event);
int ui_manager_init(void);

int ui_manager_load_acr_anim(uint8_t view_id, lv_scr_load_anim_t anim_type, uint32_t time, uint32_t delay, bool auto_del);

#ifdef __cplusplus
}
#endif

#endif /* _UI_MANAGER_H_ */
